<template>
  <el-dialog
    :title="!dataForm.complainId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="投诉人" prop="complainUserId">
      <el-input v-model="dataForm.complainUserId" placeholder="投诉人"></el-input>
    </el-form-item>
    <el-form-item label="投诉时间" prop="complainTime">
      <el-input v-model="dataForm.complainTime" placeholder="投诉时间"></el-input>
    </el-form-item>
    <el-form-item label="投诉内容" prop="complainMsg">
      <el-input v-model="dataForm.complainMsg" placeholder="投诉内容"></el-input>
    </el-form-item>
    <el-form-item label="投诉人电话" prop="complainTel">
      <el-input v-model="dataForm.complainTel" placeholder="投诉人电话"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          complainId: 0,
          complainUserId: '',
          complainTime: '',
          complainMsg: '',
          complainTel: ''
        },
        dataRule: {
          complainUserId: [
            { required: true, message: '投诉人不能为空', trigger: 'blur' }
          ],
          complainTime: [
            { required: true, message: '投诉时间不能为空', trigger: 'blur' }
          ],
          complainMsg: [
            { required: true, message: '投诉内容不能为空', trigger: 'blur' }
          ],
          complainTel: [
            { required: true, message: '投诉人电话不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.complainId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.complainId) {
            this.$http({
              url: this.$http.adornUrl(`/rent/complain/info/${this.dataForm.complainId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.complainUserId = data.complain.complainUserId
                this.dataForm.complainTime = data.complain.complainTime
                this.dataForm.complainMsg = data.complain.complainMsg
                this.dataForm.complainTel = data.complain.complainTel
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/rent/complain/${!this.dataForm.complainId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'complainId': this.dataForm.complainId || undefined,
                'complainUserId': this.dataForm.complainUserId,
                'complainTime': this.dataForm.complainTime,
                'complainMsg': this.dataForm.complainMsg,
                'complainTel': this.dataForm.complainTel
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
